package controllers.activity;

import activity.Activity;
import activity.ActivityProduct;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import enums.ShowPosition;
import helper.AliyunOssUploader;
import jodd.util.StringUtil;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import play.Logger;
import play.mvc.Controller;
import play.mvc.With;
import util.common.DateUtil;

import java.io.File;
import java.util.*;

/**
 * Created by youliangcheng on 17/9/16.
 */
@With(Secure.class)
public class ActivityCtrl extends Controller {

    public static void index(ShowPosition showPosition, long activityId) {

        Logger.info("--------------------activityId:%s", activityId);
         /*
         * 1:轮播图(INDEX_BLOCK)
         * 2:头条(INDEX_NOTICE)
         * 3:秒杀(INDEX_SECKILL)
         * 4:活动产品(INDEX_PROMOTION)
         * 5:宣传活动(INDEX_PRODUCT_RECOMMEND)
         */

        List<Activity> recommendActivityList = new ArrayList<>();

        List<Activity> allActivityList = Activity.loadActivityByMerchant(0);
        if (allActivityList != null && allActivityList.size() > 0) {

            for (Activity activity : allActivityList) {
                if (activity.showPosition == ShowPosition.INDEX_BLOCK) {
                    renderArgs.put("blockActivity", activity);
                } else if (activity.showPosition == ShowPosition.INDEX_NOTICE) {
                    renderArgs.put("noticeActivity", activity);
                } else if (activity.showPosition == ShowPosition.INDEX_SECKILL) {
                    renderArgs.put("seckillActivity", activity);
                } else if (activity.showPosition == ShowPosition.INDEX_PROMOTION) {
                    renderArgs.put("promotionActivity", activity);
                } else if (activity.showPosition == ShowPosition.INDEX_PRODUCT_RECOMMEND) {
                    recommendActivityList.add(activity);
                }else  if( activity.showPosition == ShowPosition.MERCHANT_RECOMMEND){
                    renderArgs.put("recommendMerchants", activity);
                }
            }
            renderArgs.put("recommendActivityList", recommendActivityList);
        }

        render (showPosition, activityId);
    }

    //编辑
    public static void editPage() {
        render();
    }

    //加载活动和活动产品
    @SkipAuth
    public static void getActivityAndProducts(ShowPosition showPosition) {
        Logger.info("LOGGER   000000037------getActivityAndProducts  showPosition :%s",showPosition);
        Map<String, Object> resultMap = new HashMap<>();
        Activity activity = new Activity();
        List<ActivityProduct> productList = null;
        activity = Activity.findByPosition(showPosition);
        if(activity != null){
            productList = activity != null ? ActivityProduct.findByActivityProduct(activity.id) : null;
            if (activity != null && showPosition.equals(ShowPosition.INDEX_SECKILL)) {
                activity.beginAtStr = DateUtil.dateToString(activity.beginAt, "yyyy-MM-dd");
                activity.endAtStr = DateUtil.dateToString(activity.endAt, "yyyy-MM-dd");
            }
            resultMap.put("activity", activity);
            resultMap.put("productList", productList);
        }
        renderJSON(resultMap);
    }

    //保存轮播图
    @SkipAuth
    public static void saveBlocks(
            ActivityProduct product1,
            File imageFile1,
            ActivityProduct product2,
            File imageFile2,
            ActivityProduct product3,
            File imageFile3) {


        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveBlocks 0000000009  loginUser :%s", loginUser);

//        if (imageFile1 == null && imageFile2 == null && imageFile3 == null) {
//            flash.put("msg", "请至少上传一张轮播图!!!");
//            redirect("/activity/ActivityCtrl/index");
//        }

        Activity blockActivity = Activity.findByPosition(ShowPosition.INDEX_BLOCK);
        if (blockActivity == null) {
            blockActivity = new Activity();
            blockActivity.deleted = DeletedStatus.UN_DELETED;
            blockActivity.createAt = new Date();
            blockActivity.creator = loginUser;
            blockActivity.showPosition = ShowPosition.INDEX_BLOCK;
        } else {
            blockActivity.modifyAt = new Date();
            blockActivity.modifier = loginUser;
        }
        blockActivity.save();
        if (imageFile1 != null) {
            Logger.info("LOGGER saveBlocks 0000000010----------  product1 :%s", product1.id);
            if (product1 != null && product1.id != null) {
                product1.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile1);
                product1.modifyAt = new Date();
                product1.modifier = loginUser;
            } else {
                product1 = new ActivityProduct();
                product1.activity = blockActivity;
                product1.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile1);
                product1.creator = loginUser;
                product1.createAt = new Date();
                product1.deleted = DeletedStatus.UN_DELETED;

            }
            product1.save();
        }else if(product1 != null && product1.id != null){
            product1.modifyAt = new Date();
            product1.modifier = loginUser;
            product1.save();
        }

        if (imageFile2 != null) {
            if (product2 != null && product2.id != null) {
                product2.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile2);
                product2.modifyAt = new Date();
                product2.modifier = loginUser;
            } else {
                product2 = new ActivityProduct();
                product2.activity = blockActivity;
                product2.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile2);
                product2.createAt = new Date();
                product2.creator = loginUser;
                product2.deleted = DeletedStatus.UN_DELETED;

            }
            product2.save();
        }else if(product2 != null && product2.id != null){
            product2.modifyAt = new Date();
            product2.modifier = loginUser;
            product2.save();
        }


        if (imageFile3 != null) {
            if (product3 != null && product3.id != null) {
                product3.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile3);
                product3.modifyAt = new Date();
                product3.modifier = loginUser;
            } else {
                product3 = new ActivityProduct();
                product3.imgSrc = AliyunOssUploader.uploadPublicFile(imageFile3);
                product3.activity = blockActivity;
                product3.creator = loginUser;
                product3.createAt = new Date();
                product3.deleted = DeletedStatus.UN_DELETED;

            }
            product3.save();
        }else if(product3 != null && product3.id != null){
            product3.modifyAt = new Date();
            product3.modifier = loginUser;
            product3.save();
        }

        flash.put("success", true);
        flash.put("msg", "轮播图保存成功!");
        redirect("/activity/ActivityCtrl/index");

    }

    //保存头条
    @SkipAuth
    public static void saveNotice(
            ActivityProduct product1,
            ActivityProduct product2,
            ActivityProduct product3) {

        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveNotice 0000000010  loginUser :%s", loginUser);
        Activity noticeActivity = Activity.findByPosition(ShowPosition.INDEX_NOTICE);
        if (noticeActivity == null) {
            noticeActivity = new Activity();
            noticeActivity.deleted = DeletedStatus.UN_DELETED;
            noticeActivity.createAt = new Date();
            noticeActivity.creator = loginUser;
            noticeActivity.showPosition = ShowPosition.INDEX_NOTICE;

        } else {
            noticeActivity.modifyAt = new Date();
            noticeActivity.modifier = loginUser;
        }
        noticeActivity.save();

        ActivityProduct.saveActivityProductForNotice(noticeActivity , product1 , loginUser);
        ActivityProduct.saveActivityProductForNotice(noticeActivity , product2 , loginUser);
        ActivityProduct.saveActivityProductForNotice(noticeActivity , product3 , loginUser);

        flash.put("success", true);
        flash.put("msg", "头条保存成功!");

        redirect("/activity/ActivityCtrl/index");

    }

    //保存秒杀活动
    @SkipAuth
    public static void saveSeckll(Activity activity) {


        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveSeckll 0000000057   loginUser :%s",  loginUser);

        if (activity.id == null) {
            activity.deleted = DeletedStatus.UN_DELETED;
            activity.createAt = new Date();
            activity.creator = loginUser;
            activity.showPosition = ShowPosition.INDEX_SECKILL;

        } else {
            activity.modifyAt = new Date();
            activity.modifier = loginUser;
        }
        activity.beginAt = DateUtil.stringToDate(activity.beginAtStr, "yyyy-MM-dd");
        activity.endAt = DateUtil.stringToDate(activity.endAtStr, "yyyy-MM-dd");
        activity.save();

        flash.put("success", true);
        flash.put("msg", "秒杀活动保存成功!");

        redirect("/activity/ActivityCtrl/index?showPosition=INDEX_SECKILL");

    }

    //保存秒杀产品
    @SkipAuth
    public static void saveSeckllProduct(ActivityProduct activityProduct){

        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveSeckllProduct 0000000059   loginUser :%s",  loginUser);

        if (activityProduct.id == null) {
            activityProduct.deleted = DeletedStatus.UN_DELETED;
            activityProduct.creator = loginUser;
            activityProduct.createAt = new Date();
            activityProduct.save();
        } else {
            activityProduct.modifier = loginUser;
            activityProduct.modifyAt = new Date();
            activityProduct.save();
        }

        flash.put("success", true);
        flash.put("msg", "秒杀产品保存成功!");

        redirect("/activity/ActivityCtrl/index?showPosition=INDEX_SECKILL");
    }

    //删除秒杀产品
    @SkipAuth
    public static void deletedSeckillProduct(Long activityProductId){
        Logger.info("LOGGER  deletedSeckillProduct    2017101900001 activityProductId :%s" , activityProductId);
        Map<String , Object> resultMap = new HashMap<>();

        ActivityProduct activityProduct = ActivityProduct.findByActivityProductId(activityProductId);
        if(activityProduct == null){
            flash.put("success", false);
            flash.put("msg", "您要删除的产品不能存在,可能已经被删除!");
            resultMap.put("success",false);
            renderJSON(resultMap);

        }
        activityProduct.deleted = DeletedStatus.DELETED ;
        Secure.setBaseInfo(activityProduct);
        activityProduct.save();
        flash.put("success", true);
        flash.put("msg", "秒杀产品删除成功!");
        resultMap.put("success",true);
        renderJSON(resultMap);

    }

    //保存活动图片
    @SkipAuth
    public static void savePromotion( File imageFile,String url) {
        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER savePromotion 0000000030  loginUser :%s , operate: %s", loginUser , "保存活动图片");

        if (imageFile == null) {
            flash.put("msg", "上传图片不能为空!");
            redirect("/activity/ActivityCtrl/index?showPosition=" + ShowPosition.INDEX_PROMOTION);
        }

        Activity promotionActivity = Activity.findByPosition(ShowPosition.INDEX_PROMOTION);
        if (promotionActivity == null) {
            promotionActivity = new Activity();
            promotionActivity.deleted = DeletedStatus.UN_DELETED;
            promotionActivity.createAt = new Date();
            promotionActivity.creator = loginUser;
            promotionActivity.showPosition = ShowPosition.INDEX_PROMOTION;

        } else {
            promotionActivity.modifyAt = new Date();
            promotionActivity.modifier = loginUser;
        }
        if(imageFile != null) {
            promotionActivity.mainImg = AliyunOssUploader.uploadPublicFile(imageFile);
        }
        if(StringUtil.isNotBlank(url)) {
            promotionActivity.url = url;
        }else{
            promotionActivity.url = null;
        }
        promotionActivity.save();


        flash.put("success", true);
        flash.put("msg", "活动图片保存成功!");

        redirect("/activity/ActivityCtrl/index?showPosition=" + ShowPosition.INDEX_PROMOTION);


    }

    //加载促销活动
    @SkipAuth
    public static void getRecomendActivity(long activityId) {
        Logger.info("LOGGER    000000067  getRecomendActivity activityId :%s", activityId);
        Map<String, Object> resultMap = new HashMap<>();

        Activity activity = Activity.findByActivityId(activityId);
        List<ActivityProduct> productList = ActivityProduct.findByActivity(activityId);
        resultMap.put("activity", activity);
        resultMap.put("productList", productList);
        renderJSON(resultMap);

    }

    //保存促活动
    @SkipAuth
    public static void saveRecomend(Activity activity,
                                    ActivityProduct product1,
                                    ActivityProduct product2,
                                    ActivityProduct product3,
                                    ActivityProduct product4,
                                    ActivityProduct product5,
                                    ActivityProduct product6,
                                    File imageFile) {
        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveRecomend 0000000037  loginUser :%s , operate: %s", loginUser , "保存促销活动");
        Logger.info("LOGGER saveRecomend 0000000037  activity.id :%s , operate: %s", activity.id , "保存促销活动Id");
        if (activity.id == null) {
            activity.createAt = new Date();
            activity.deleted = DeletedStatus.UN_DELETED;
            activity.creator = loginUser;
            activity.showPosition = ShowPosition.INDEX_PRODUCT_RECOMMEND;
        } else {
            activity.modifyAt = new Date();
            activity.modifier = loginUser;
        }
        if(imageFile != null) {
            activity.mainImg = AliyunOssUploader.uploadPublicFile(imageFile);
        }
        activity.save();
        //保存产品
        ActivityProduct.saveActivityProductForProduct(activity , product1 , loginUser);
        ActivityProduct.saveActivityProductForProduct(activity , product2 , loginUser);
        ActivityProduct.saveActivityProductForProduct(activity , product3 , loginUser);
        ActivityProduct.saveActivityProductForProduct(activity , product4 , loginUser);
        ActivityProduct.saveActivityProductForProduct(activity , product5 , loginUser);
        ActivityProduct.saveActivityProductForProduct(activity , product6 , loginUser);



        flash.put("success", true);
        flash.put("msg", "促销活动保存成功!");

        redirect("/activity/ActivityCtrl/index?showPosition=" + ShowPosition.INDEX_PRODUCT_RECOMMEND + "&activityId=" + activity.id);


    }

    //删除活动
    @SkipAuth
    public static void deletedRecomend(long activityId){

        Logger.info("LOGGER ----deletedRecomend 20171012000001 删除活动 , 活动ID : %s" , activityId);
        Map<String , Object> resultMap = new HashMap<>();
        Activity activity = Activity.findByActivityId(activityId);
        if(activity == null){
            flash.put("success", false);
            flash.put("msg", "您要删除的活动不存在,可能已经被删除!");
            resultMap.put("success",false);
            renderJSON(resultMap);

        }
        activity.deleted = DeletedStatus.DELETED;
        Secure.setBaseInfo(activity);
        activity.save();

        flash.put("success", true);
        flash.put("msg", "删除成功!");
        resultMap.put("success",true);
        renderJSON(resultMap);


    }

    //保存推荐的商户
    public static void saveRecommendMerchants(Activity activity,
                                              ActivityProduct product1,
                                              ActivityProduct product2,
                                              ActivityProduct product3,
                                              ActivityProduct product4,
                                              ActivityProduct product5,
                                              ActivityProduct product6 ){
        AdminUser loginUser = Secure.getLoginUser();
        Logger.info("LOGGER saveRecommendMerchants 0000000097  loginUser :%s , operate: %s", loginUser , "保存推荐商户");

        if(activity.id == null ){
            activity = new Activity();
            activity.deleted = DeletedStatus.UN_DELETED ;
            activity.showPosition = ShowPosition.MERCHANT_RECOMMEND ;

        }
        Secure.setBaseInfo(activity);
        activity.save();

        //保存上传的商户
        ActivityProduct.saveActivityProductForMerchant(activity , product1 , loginUser);
        ActivityProduct.saveActivityProductForMerchant(activity , product2 , loginUser);
        ActivityProduct.saveActivityProductForMerchant(activity , product3 , loginUser);
        ActivityProduct.saveActivityProductForMerchant(activity , product4 , loginUser);
        ActivityProduct.saveActivityProductForMerchant(activity , product5 , loginUser);
        ActivityProduct.saveActivityProductForMerchant(activity , product6 , loginUser);

        flash.put("success", true);
        flash.put("msg", "保存成功!");

        redirect("/activity/ActivityCtrl/index?showPosition=" + ShowPosition.MERCHANT_RECOMMEND + "&activityId=" + activity.id);

    }


}
